import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc
from scipy import interp
#111
death_records = pd.read_csv('DeathRecords.csv')
death_records_columns = death_records.columns
death_records = death_records[death_records['EducationReportingFlag'] == 1]
death_records = death_records.drop(['EducationReportingFlag','Education1989Revision'], axis=1)



#222
gun_homicides = death_records[death_records['CauseRecode113']==128]
gun_suicides = death_records[death_records['CauseRecode113']==125]

gun_deaths = pd.concat([gun_homicides,gun_suicides])
gun_deaths['CauseRecode113'] = gun_deaths['CauseRecode113'].map({128: 1, 125: 0})
gun_deaths = gun_deaths.drop(['CauseRecode39','CauseRecode358',
                              'InfantCauseRecode130','PlaceOfDeathAndDecedentsStatus',
                             'MethodOfDisposition','Autopsy',
                             'NumberOfEntityAxisConditions',
                             'NumberOfRecordAxisConditions','AgeRecode27',
                             'AgeRecode52','AgeSubstitutionFlag','AgeType',
                             'InfantAgeRecode22','CurrentDataYear',
                             'MannerOfDeath','Icd10Code','Age'],axis=1)
gun_deaths=gun_deaths.rename(columns = {'CauseRecode113':'Homicide_1',
                                        'Education2003Revision':'Edu',
                                       'MonthOfDeath':'DeathMonth',
                                        'DayOfWeekOfDeath':'DeathDay',
                                        'AgeRecode12': 'Age'
                                       })

gun_deaths = gun_deaths[gun_deaths['RaceImputationFlag']==0]

gun_deaths = gun_deaths.drop(['RaceImputationFlag'],axis=1)
gun_deaths = gun_deaths[gun_deaths['InjuryAtWork'] != 'U']
gun_deaths['InjuryAtWork'] = gun_deaths['InjuryAtWork'].map({'Y': 1, 'N': 0})
gun_deaths['Sex'] = gun_deaths['Sex'].map({'M':0, 'F':1})
gun_deaths = gun_deaths[gun_deaths['BridgedRaceFlag']==0]
gun_deaths = gun_deaths.drop('BridgedRaceFlag',axis=1)
gun_deaths = gun_deaths[gun_deaths['Edu'] < 9]
gun_deaths = gun_deaths[gun_deaths['MaritalStatus'] != 'U']
gun_deaths = gun_deaths[gun_deaths['Age'] < 12]
gun_deaths = gun_deaths[gun_deaths['HispanicOrigin'] < 996]
gun_deaths = gun_deaths[gun_deaths['HispanicOriginRaceRecode'] < 9]
gun_deaths['Intercepts'] = np.ones(len(gun_deaths))

resident_dummies = pd.get_dummies(gun_deaths['ResidentStatus'], prefix='ResidentStatus')
#residence bassline is 1- resident of state/county where death occurred
edu_dummies = pd.get_dummies(gun_deaths['Edu'], prefix='Edu')
#edu bassline is 1- 8th grade or less
age_dummies = pd.get_dummies(gun_deaths['Age'], prefix='Age')
#age bassline is 1- less than a year old
marital_dummies = pd.get_dummies(gun_deaths['MaritalStatus'], prefix='MaritalStatus')
#marital bassline is D-divorced
injury_dummies = pd.get_dummies(gun_deaths['PlaceOfInjury'], prefix='PlaceOfInjury')
#injury bassline is 0- occurred at home
activity_dummies = pd.get_dummies(gun_deaths['ActivityCode'],prefix='ActivityCode')
#activity bassline is 0- occurred during sports
race_dummies = pd.get_dummies(gun_deaths['Race'],prefix='Race')
#race bassline is 1-white
race3_dummies = pd.get_dummies(gun_deaths['RaceRecode3'],prefix='RaceRecode3')
#race3 bassline is 1-white
race5_dummies = pd.get_dummies(gun_deaths['RaceRecode5'],prefix='RaceRecode5')
#race5 bassline is 1-White
hisp_dummies = pd.get_dummies(gun_deaths['HispanicOrigin'],prefix='HispanicOrigin')
#hispanic bassline is 100-nonhispanic
hisprecode_dummies = pd.get_dummies(gun_deaths['HispanicOriginRaceRecode'],prefix='HispanicOriginRaceRecode')
#hispanicrecode bassline is 1-mexican


cols_to_keep = ['Id', 'Sex', 'Intercepts', 'DeathMonth', 'DeathDay', 'Homicide_1']
dummy_data = gun_deaths[cols_to_keep].join(resident_dummies.ix[:, 'ResidentStatus_2':])
dummy_data = dummy_data.join(edu_dummies.ix[:,'Edu_2':])
dummy_data = dummy_data.join(age_dummies.ix[:,'Age_2':])
dummy_data = dummy_data.join(marital_dummies.ix[:,'MaritalStatus_M':])
dummy_data = dummy_data.join(injury_dummies.ix[:,'PlaceOfInjury_2':])
dummy_data = dummy_data.join(activity_dummies.ix[:,'ActivityCode_2':])
dummy_data = dummy_data.join(race_dummies.ix[:,'Race_2':])
dummy_data = dummy_data.join(race3_dummies.ix[:,'RaceRecode3_2':])
dummy_data = dummy_data.join(race5_dummies.ix[:,'RaceRecode5_2':])
dummy_data = dummy_data.join(hisp_dummies.ix[:,'HispanicOrigin_200':])
dummy_data = dummy_data.join(hisprecode_dummies.ix[:,'HispanicOriginRaceRecode_2':])

print 'tongji'
print(len(gun_suicides),len(gun_homicides), len(dummy_data))

#333
from sklearn.cross_validation import train_test_split as tts


features_list = ['Intercepts', 'ResidentStatus_2','ResidentStatus_3', 'ResidentStatus_4', 
'Edu_2', 'Edu_3', 'Edu_4', 'Edu_5', 'Edu_6', 'Edu_7', 'Edu_8',
'Age_2', 'Age_3', 'Age_4', 'Age_5', 'Age_6', 'Age_7', 'Age_8','Age_9', 'Age_10', 'Age_11',
'MaritalStatus_M', 'MaritalStatus_S', 'MaritalStatus_W',
'PlaceOfInjury_2', 'PlaceOfInjury_3', 'PlaceOfInjury_4', 'PlaceOfInjury_5',
'PlaceOfInjury_6', 'PlaceOfInjury_7', 'PlaceOfInjury_8', 'PlaceOfInjury_9',
'ActivityCode_2', 'ActivityCode_4', 'ActivityCode_8', 'ActivityCode_9', 
'HispanicOriginRaceRecode_2', 'HispanicOriginRaceRecode_3',
'HispanicOriginRaceRecode_4', 'HispanicOriginRaceRecode_5',
'HispanicOriginRaceRecode_6', 'HispanicOriginRaceRecode_7',
'HispanicOriginRaceRecode_8']

X = dummy_data[['Intercepts','Sex', 'ResidentStatus_2','ResidentStatus_3', 'ResidentStatus_4', 
'Edu_2', 'Edu_3', 'Edu_4', 'Edu_5', 'Edu_6', 'Edu_7', 'Edu_8',
'Age_2', 'Age_3', 'Age_4', 'Age_5', 'Age_6', 'Age_7', 'Age_8','Age_9', 'Age_10', 'Age_11',
'MaritalStatus_M', 'MaritalStatus_S', 'MaritalStatus_W',
'PlaceOfInjury_2', 'PlaceOfInjury_3', 'PlaceOfInjury_4', 'PlaceOfInjury_5',
'PlaceOfInjury_6', 'PlaceOfInjury_7', 'PlaceOfInjury_8', 'PlaceOfInjury_9',
'ActivityCode_2', 'ActivityCode_4', 'ActivityCode_8', 'ActivityCode_9', 
'HispanicOriginRaceRecode_2', 'HispanicOriginRaceRecode_3',
'HispanicOriginRaceRecode_4', 'HispanicOriginRaceRecode_5',
'HispanicOriginRaceRecode_6', 'HispanicOriginRaceRecode_7',
'HispanicOriginRaceRecode_8']]



x_vars = ['Intercepts','Sex', 'SameState','SameCountry', 'ForeignBorn', 
'9_12grade', 'HS_grad', 'CollegeCredit', 'Assoc_Deg', 'Bach_Deg', 'Master_Deg', 'Doctorate',
'1_4_yr', '5_14_yr', '15_24_yr', '25_34_yr', '35_44_yr',
          '45_54_yr', '55_64_yr','65_74_yr','75_84_yr', '85_plus_yr',
'Married', 'Single', 'Widowed',
'school', 'sports_area', 'road', 'trade_area',
'industrial_area', 'farm', 'other', 'unknown',
'working', 'resting', 'other', 'unknown', 
'puerto_rican', 'cuban',
'central_south_american', 'other_unknown_hisp',
'white', 'black',
'other']

y = dummy_data['Homicide_1']

x_train, x_test, y_train, y_test = tts(X,y, test_size=0.50, random_state=42)

from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics

mean_tpr = 0.0
mean_fpr = np.linspace(0, 1, 100)
all_tpr = []


clf = RandomForestClassifier(n_estimators = 1000)
probas_= clf.fit(x_train , y_train).predict_proba(x_test)
# Compute ROC curve and area the curve

fpr, tpr, thresholds = roc_curve(y_test, probas_[:, 1])
mean_tpr += interp(mean_fpr, fpr, tpr)          
mean_tpr[0] = 0.0                      
roc_auc = auc(fpr, tpr)



plt.plot([0, 1], [0, 1], '--', color=(0.6, 0.6, 0.6), label='Luck')
                 
mean_tpr[-1] = 1.0                        
mean_auc = auc(mean_fpr, mean_tpr)        

plt.plot(mean_fpr, mean_tpr, 'k--',
label='Mean ROC (area = %0.2f)' % mean_auc, lw=2,color="red")

plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic example')
plt.legend(loc="lower right")
plt.show()